package com.yx1996.day_02;

import com.yx1996.domain.Customer;
import com.yx1996.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import java.util.List;

/**
 * 适用于单表查询
 */
public class testCriteria {
    @Test
    //Criteria基本查询
    public void fun1(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 开启事务
        Transaction tx = session.beginTransaction();
        //3 执行查询
        //----------------------------------------------------------------
        Criteria criteria = session.createCriteria(Customer.class);
        List<Customer> list = criteria.list();
        // Customer o = (Customer) query.uniqueResult();  返回单个结果
        //7 输出查询结果
        System.out.println(list);
        //----------------------------------------------------------------
        //8 提交事务.关闭资源
        tx.commit();
        session.close();// 游离|托管 状态, 有id , 没有关联

    }
    @Test
    //Criteria条件查询  注意hibernate查询中不会出现所有关于数据库的信息
    //条件查询
    //HQL语句中,不可能出现任何数据库相关的信息的
    // > 				gt
    // >=				ge
    // <				lt
    // <=				le
    // ==				eq
    // !=				ne
    // in				in
    // between and		between
    // like 			like
    // is not null 		isNotNull
    // is null			isNull
    // or				or
    // and				and
    public void fun2(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 开启事务
        Transaction tx = session.beginTransaction();
        //3 执行查询
        //----------------------------------------------------------------
        Criteria criteria = session.createCriteria(Customer.class);
        criteria.add(Restrictions.eq("cust_id",10l));
        //6 通过query获得查询结果（两种方式）
        Customer c = (Customer) criteria.uniqueResult();  //返回单个结果
        //7 输出查询结果
        System.out.println(c);
        //----------------------------------------------------------------
        //8 提交事务.关闭资源
        tx.commit();
        session.close();// 游离|托管 状态, 有id , 没有关联

    }

    @Test
    //Criteria条件查询  分页查询
    public void fun5(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 开启事务
        Transaction tx = session.beginTransaction();
        //3 执行查询
        //----------------------------------------------------------------
        Criteria criteria = session.createCriteria(Customer.class);
        criteria.setFirstResult(0);
        criteria.setMaxResults(2);
        List<Customer> list = criteria.list();

        System.out.println(list);
        //----------------------------------------------------------------
        //8 提交事务.关闭资源
        tx.commit();
        session.close();// 游离|托管 状态, 有id , 没有关联

    }
    @Test
    //Criteria条件查询  总条数查询
    public void fun6(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 开启事务
        Transaction tx = session.beginTransaction();
        //3 执行查询
        //----------------------------------------------------------------
        //创建Criteria对象
        Criteria criteria = session.createCriteria(Customer.class);
        //总条数查询
        criteria.setProjection(Projections.rowCount());
        //获得查询结果
        Long rows = (Long) criteria.uniqueResult();

        System.out.println(rows);
        //----------------------------------------------------------------
        //8 提交事务.关闭资源
        tx.commit();
        session.close();// 游离|托管 状态, 有id , 没有关联

    }
}
